/**
 * ChronosPagePanel.java
 *
 * Copyright (c) 2011 Daniel Berenguer <dberenguer@usapiens.com>
 *
 * This file is part of the panStamp project.
 *
 * panStamp  is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * any later version.
 *
 * panLoader is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with panLoader; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 *
 * Author: Daniel Berenguer
 * Creation date: 06/16/2011
 */

package swapdmt;

import swap.SwapValue;

/**
 *
 * @author daniel
 */
public class ChronosPagePanel extends javax.swing.JPanel {

  /** Creates new form ChronosPagePanel */
  public ChronosPagePanel()
  {
    initComponents();

    butGroupEndpType0.add(jRadioButtonAnalog);
    butGroupEndpType0.add(jRadioButtonBinary);

    butGroupEndpDir0.add(jRadioButtonInput);
    butGroupEndpDir0.add(jRadioButtonOutput);
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    butGroupEndpType0 = new javax.swing.ButtonGroup();
    butGroupEndpDir0 = new javax.swing.ButtonGroup();
    jLabel10 = new javax.swing.JLabel();
    jLabel11 = new javax.swing.JLabel();
    jLabel12 = new javax.swing.JLabel();
    jLabel40 = new javax.swing.JLabel();
    jTextLabel = new javax.swing.JTextField();
    jTextRegId = new javax.swing.JTextField();
    jTextRegAddr = new javax.swing.JTextField();
    jTextDevAddr = new javax.swing.JTextField();
    jRadioButtonBinary = new javax.swing.JRadioButton();
    jRadioButtonInput = new javax.swing.JRadioButton();
    jCheckBoxHasDec = new javax.swing.JCheckBox();
    jTextPosition = new javax.swing.JTextField();
    jLabel13 = new javax.swing.JLabel();
    jLabel14 = new javax.swing.JLabel();
    jTextSize = new javax.swing.JTextField();
    jCheckBoxShowSign = new javax.swing.JCheckBox();
    jRadioButtonOutput = new javax.swing.JRadioButton();
    jRadioButtonAnalog = new javax.swing.JRadioButton();
    jCheckBoxCombined = new javax.swing.JCheckBox();
    jTextDelta = new javax.swing.JTextField();
    jLabel15 = new javax.swing.JLabel();

    setName("Form"); // NOI18N

    org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(swapdmt.SWAPdmtApp.class).getContext().getResourceMap(ChronosPagePanel.class);
    jLabel10.setText(resourceMap.getString("jLabel10.text")); // NOI18N
    jLabel10.setName("jLabel10"); // NOI18N

    jLabel11.setText(resourceMap.getString("jLabel11.text")); // NOI18N
    jLabel11.setName("jLabel11"); // NOI18N

    jLabel12.setText(resourceMap.getString("jLabel12.text")); // NOI18N
    jLabel12.setName("jLabel12"); // NOI18N

    jLabel40.setText(resourceMap.getString("jLabel40.text")); // NOI18N
    jLabel40.setName("jLabel40"); // NOI18N

    jTextLabel.setName("jTextLabel"); // NOI18N

    jTextRegId.setName("jTextRegId"); // NOI18N

    jTextRegAddr.setName("jTextRegAddr"); // NOI18N

    jTextDevAddr.setName("jTextDevAddr"); // NOI18N

    jRadioButtonBinary.setText(resourceMap.getString("jRadioButtonBinary.text")); // NOI18N
    jRadioButtonBinary.setName("jRadioButtonBinary"); // NOI18N

    jRadioButtonInput.setSelected(true);
    jRadioButtonInput.setText(resourceMap.getString("jRadioButtonInput.text")); // NOI18N
    jRadioButtonInput.setName("jRadioButtonInput"); // NOI18N

    jCheckBoxHasDec.setText(resourceMap.getString("jCheckBoxHasDec.text")); // NOI18N
    jCheckBoxHasDec.setName("jCheckBoxHasDec"); // NOI18N

    jTextPosition.setText(resourceMap.getString("jTextPosition.text")); // NOI18N
    jTextPosition.setName("jTextPosition"); // NOI18N

    jLabel13.setText(resourceMap.getString("jLabel13.text")); // NOI18N
    jLabel13.setName("jLabel13"); // NOI18N

    jLabel14.setText(resourceMap.getString("jLabel14.text")); // NOI18N
    jLabel14.setName("jLabel14"); // NOI18N

    jTextSize.setText(resourceMap.getString("jTextSize.text")); // NOI18N
    jTextSize.setName("jTextSize"); // NOI18N

    jCheckBoxShowSign.setText(resourceMap.getString("jCheckBoxShowSign.text")); // NOI18N
    jCheckBoxShowSign.setName("jCheckBoxShowSign"); // NOI18N

    jRadioButtonOutput.setText(resourceMap.getString("jRadioButtonOutput.text")); // NOI18N
    jRadioButtonOutput.setName("jRadioButtonOutput"); // NOI18N

    jRadioButtonAnalog.setSelected(true);
    jRadioButtonAnalog.setText(resourceMap.getString("jRadioButtonAnalog.text")); // NOI18N
    jRadioButtonAnalog.setName("jRadioButtonAnalog"); // NOI18N

    jCheckBoxCombined.setText(resourceMap.getString("jCheckBoxCombined.text")); // NOI18N
    jCheckBoxCombined.setName("jCheckBoxCombined"); // NOI18N

    jTextDelta.setText(resourceMap.getString("jTextDelta.text")); // NOI18N
    jTextDelta.setName("jTextDelta"); // NOI18N

    jLabel15.setText(resourceMap.getString("jLabel15.text")); // NOI18N
    jLabel15.setName("jLabel15"); // NOI18N

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
          .addGroup(layout.createSequentialGroup()
            .addComponent(jLabel10)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jTextDevAddr))
          .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(jLabel11)
              .addComponent(jLabel40))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
              .addComponent(jTextRegId, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE)
              .addComponent(jTextRegAddr, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE)
              .addComponent(jTextLabel, javax.swing.GroupLayout.Alignment.TRAILING)))
          .addComponent(jLabel12))
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addGap(34, 34, 34)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(jRadioButtonInput)
              .addComponent(jRadioButtonBinary)
              .addComponent(jCheckBoxHasDec))
            .addGap(42, 42, 42))
          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addGap(12, 12, 12)
            .addComponent(jLabel13)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jTextPosition, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jLabel14)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(jRadioButtonOutput)
          .addComponent(jRadioButtonAnalog)
          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(layout.createSequentialGroup()
              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
              .addComponent(jTextSize, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
              .addComponent(jLabel15)
              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
              .addComponent(jTextDelta, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
              .addGap(22, 22, 22))
            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
              .addComponent(jCheckBoxShowSign)
              .addGap(18, 18, 18)
              .addComponent(jCheckBoxCombined))))
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addGap(20, 20, 20)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel10)
          .addComponent(jTextDevAddr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jRadioButtonBinary)
          .addComponent(jRadioButtonAnalog))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel11)
          .addComponent(jTextRegAddr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jRadioButtonInput)
          .addComponent(jRadioButtonOutput))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel12)
          .addComponent(jTextRegId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jCheckBoxHasDec)
          .addComponent(jCheckBoxShowSign)
          .addComponent(jCheckBoxCombined))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jTextSize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jLabel14)
          .addComponent(jTextPosition, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jTextLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jLabel40)
          .addComponent(jLabel13)
          .addComponent(jLabel15)
          .addComponent(jTextDelta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
  }// </editor-fold>//GEN-END:initComponents


  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.ButtonGroup butGroupEndpDir0;
  private javax.swing.ButtonGroup butGroupEndpType0;
  private javax.swing.JCheckBox jCheckBoxCombined;
  private javax.swing.JCheckBox jCheckBoxHasDec;
  private javax.swing.JCheckBox jCheckBoxShowSign;
  private javax.swing.JLabel jLabel10;
  private javax.swing.JLabel jLabel11;
  private javax.swing.JLabel jLabel12;
  private javax.swing.JLabel jLabel13;
  private javax.swing.JLabel jLabel14;
  private javax.swing.JLabel jLabel15;
  private javax.swing.JLabel jLabel40;
  private javax.swing.JRadioButton jRadioButtonAnalog;
  private javax.swing.JRadioButton jRadioButtonBinary;
  private javax.swing.JRadioButton jRadioButtonInput;
  private javax.swing.JRadioButton jRadioButtonOutput;
  private javax.swing.JTextField jTextDelta;
  private javax.swing.JTextField jTextDevAddr;
  private javax.swing.JTextField jTextLabel;
  private javax.swing.JTextField jTextPosition;
  private javax.swing.JTextField jTextRegAddr;
  private javax.swing.JTextField jTextRegId;
  private javax.swing.JTextField jTextSize;
  // End of variables declaration//GEN-END:variables

  /**
   * getPage
   *
   * Return page SwapValue, ready to be sent to the Chronos
   */
  public SwapValue getPage()
  {
    int[] arr = new int[11];
    String buf = jTextLabel.getText().toUpperCase();
    int i;

    // Endpoint label
    if (buf.equals(""))
      return null;
    int len = 5;
    if (buf.length() < len)
      len = buf.length();
    for(i=0 ; i<len ; i++)
      arr[i] = buf.charAt(i);
    for(i=len ; i<5 ; i++)
      arr[i] = ' ';

    // Device address
    if ((buf = jTextDevAddr.getText()) == null)
      return null;
    int val = Integer.parseInt(buf);
    if (val < 1)
      return null;
    arr[5] = val;

    // Register address
    if ((buf = jTextRegAddr.getText()) == null)
      return null;
    val = Integer.parseInt(buf);
    if (val < 0)
      return null;
    arr[6] = val;

    // Register ID
    if ((buf = jTextRegId.getText()) == null)
      return null;
    val = Integer.parseInt(buf);
    if (val < 0)
      return null;
    arr[7] = val;

    // Format
    val = 0;
    if (jRadioButtonAnalog.isSelected())
      val |= 0x01;
    if (jRadioButtonOutput.isSelected())
      val |= 0x02;
    if (jCheckBoxHasDec.isSelected())
      val |= 0x04;
    if (jCheckBoxShowSign.isSelected())
      val |= 0x08;
    if (jCheckBoxCombined.isSelected())
      val |= 0x10;
    arr[8] = val;

    // Delta
    if ((buf = jTextDelta.getText()) == null)
      return null;
    val = Integer.parseInt(buf);
    if (val < 0)
      return null;
    arr[9] = val;

    // Position and size
    if ((buf = jTextPosition.getText()) == null)
      return null;
    val = Integer.parseInt(buf);
    if ((buf = jTextSize.getText()) == null)
      return null;
    int size = Integer.parseInt(buf);
    if (val < 0 || size < 1)
      return null;
    arr[10] = (size << 6) | (val & 0x3F);

    return new SwapValue(arr);
  }

  /**
   * zeroPadder
   *
   * Left-pad with zeroes a given hex string
   *
   * 'hexStr' Hex string to be padded
   * 'len'    Final length of the hex string to be returned
   *
   * Returns:
   *  Hex string with the required length, after padding with zeroes
   */
  private String zeroPadder(String hexStr, int len)
  {
    int i;
    StringBuilder strBuf = new StringBuilder(hexStr);

    for (i=0 ; i < (len - strBuf.length()) ; i++)
      strBuf.insert(0, '0');

    return strBuf.toString();
  }
}
